﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Socket服务器
{
    /// <summary>
    /// AddServer.xaml 的交互逻辑
    /// </summary>
    public partial class AddServers : Window
    {
        public AddServers(ServerSource sm)
        {
            InitializeComponent();
            InsertIp();
            serversource = sm;
        }
        ServerSource serversource;


        /// <summary>
        /// 获取IPV4地址
        /// </summary>
        /// <returns></returns>
        List<System.Net.IPAddress> GetLocalIpv4()
        {
            IPAddress[] localIPs;
            localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            List<IPAddress> IPV4 = new List<IPAddress>();
            foreach (IPAddress ip in localIPs)
            {
                //根据AddressFamily判断是否为ipv4,如果是InterNetWorkV6则为ipv6  
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    IPV4.Add(ip);
            }
            return IPV4;
        }

        List<System.Net.IPAddress> IPAdress = new List<System.Net.IPAddress>();

        void InsertIp()
        {
            IPAdress = GetLocalIpv4();
            for (int i = 0; i < IPAdress.Count; i++)
            {
                string ip = IPAdress[i].ToString();
                IP.Items.Add(ip);
            }
            if (IP.Items.Count > 0)
                IP.SelectedIndex = 0;
        }

        #region 判断是不是IP地址
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        public static bool IsIPSect(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");
        }
        #endregion
        /// <summary>
        /// 判断字符串是不是数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsInteger(string s)
        {
            string pattern = @"^\d*$";
            return Regex.IsMatch(s, pattern);
        }

        private int  CheckRule()
        {
            if (!IsIPSect(IP.Text))
                return 1;
            if (!IsInteger(Port.Text))
                return 2;
            else
            {
                long  ports = Convert.ToInt64 (Port.Text);
                if (ports > 65535 || ports < 0)
                    return 2;
            }
            if (!IsInteger(TimeOut.Text))
                return 3;
            else
            {
                long DeviceCount = Convert.ToInt64(TimeOut.Text);
                if (DeviceCount > MaxConnect || DeviceCount < 0)
                    return 3;
            }
            return 0;
        }

        /// <summary>
        /// 允许最大65535个SOCKET连入
        /// </summary>
        int MaxConnect = 65535;
        private void ADD_Click(object sender, RoutedEventArgs e)
        {
            switch(CheckRule() )
            {
                case 1:
                    {
                        MessageBox.Show("IP地址输入不正确");
                        return;
                    }
                case 2:
                    {
                        MessageBox.Show("端口号范围：0-65535");
                        return;
                    }
                case 3:
                    {
                        MessageBox.Show("最大连接数范围：0-65535");
                        return;
                    }
            }
            ServerConfig SC = new ServerConfig();
            SC.ServerIP  = IPAdress[IP.SelectedIndex];
            SC.ServerPort =Convert.ToInt32 ( Port.Text) ;
            SC.CapaCity = Convert.ToInt32(TimeOut.Text);
            SC.DeviceCount = Convert.ToInt32(TimeOut.Text);
            if (ServerName.Text == "")
                SC.ServerName = SC.ServerIP.ToString();
            else
                SC.ServerName = ServerName.Text;
            if (serversource.ServerAdd(SC))
            {
                SocketManager ServerView = serversource.GetServer(SC.ServerPort);
                this.Close();
            }
            else
            {
                MessageBox.Show("端口已被占用");
            }
            
        }


        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();            
        }
    }
}
